package com.kc.sjwh.action;

import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.kc.sjwh.entity.bo.ResBo;
import com.kc.sjwh.entity.po.Res;
import com.kc.sjwh.entity.vo.table.Table;
import com.kc.sjwh.entity.vo.table.Td;
import com.kc.sjwh.entity.vo.table.Tr;
import com.kc.sjwh.other.AjaxRes;
import com.kc.sjwh.other.Page;
import com.kc.sjwh.service.AppService;
import com.kc.sjwh.service.ResService;

/**
 * 资源管理
 * @author zhangguihua(hua7381@163.com)
 * date: 2017年8月4日
 */
@Controller
@RequestMapping("res")
public class ResAction extends BaseAction {
	static Logger logger = Logger.getLogger(ResAction.class);
	
	@Autowired
	ResService service;
	@Autowired
	AppService appService;
	
	/**
	 * 打开列表页
	 * @param req
	 * @return
	 */
	@RequestMapping("list")
	public String list(HttpServletRequest req) {
		req.setAttribute("apps", appService.getAll());
		return "res/resList";
	}
	
	/**
	 * 打开资源树
	 * @param req
	 * @param yyid 所属应用id
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("tree")
	public String tree(HttpServletRequest req, String yyid, String yymc) throws Exception {
		req.setAttribute("yyid", yyid);
		req.setAttribute("yymc", URLDecoder.decode(yymc,"utf-8"));
		return "res/resTree";
	}
	
	@ResponseBody
	@RequestMapping("tree_getTree")
	public String tree_getTree(HttpServletRequest req, String yyid) {
		return JSON.toJSONString(new AjaxRes(service.tree(yyid)));
	}
	
	@ResponseBody
	@RequestMapping("tree_getOne")
	public String tree_getOne(HttpServletRequest req, String id) {
		Res one = service.getOne(id);
		return JSON.toJSONString(one);
	}
	
	/**
	 * 分页查询
	 * @param req
	 * @param pageSize
	 * @param pageNum
	 * @return
	 */
	@ResponseBody
	@RequestMapping("getPage")
	public String getPage(HttpServletRequest req, int pageSize, int pageNum) {
		Page<Res> page = service.getPage(pageSize, pageNum, super.getParamMap(req));
		
		//数据转表格table
		Table table = new Table();
		table.add(new Tr("所属应用", "资源类别", "父级资源", "资源名称", "权限代码", "地址", "操作"));//表头
		for(Res one : page.getList()) {//数据行
			ResBo bo = (ResBo)one;
			table.add(new Tr(
				new Td(bo.getYymc_()),
				new Td(bo.getLb()),
				new Td(bo.getFjmc_()),
				new Td(bo.getMc()),
				new Td(bo.getQxdm()),
				new Td(bo.getDz()),
				new Td("<a href='form?id="+one.getId()+"'>修改</a> &nbsp; "+ 
						"<a onclick=\"deletes('"+one.getId()+"')\">删除</a>")
			));
		}
		
		//返回结果
		Map data = new HashMap();
		data.put("total", page.getTotal());
		data.put("html", table.toString());
		return JSON.toJSONString(new AjaxRes(data));
	}
	
	/**
	 * 打开表单(新增或修改)
	 * @param id
	 */
	@RequestMapping("form")
	public String form(HttpServletRequest req, String id) {
		req.setAttribute("act", id==null?"新增":"修改");
		req.setAttribute("one", service.form(id));
		req.setAttribute("apps", appService.getAll());
		return "res/resForm";
	}
	
	/**
	 * 保存（新增或修改）
	 * @param one
	 */
	@ResponseBody
	@RequestMapping("save")
	public String save(HttpServletRequest req, Res one) throws Exception {
		if(StringUtils.isEmpty(one.getId())) {
			one.setId(UUID.randomUUID().toString().replace("-", ""));
			one.setCjrxm(getLoginUser().getMc());
			service.add(one);
			return JSON.toJSONString(one.getId());
		} else {
			service.update(one);
			return JSON.toJSONString(one.getId());
		}
	}
	
	/**
	 * 删除
	 * @param req
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("delete")
	public String delete(HttpServletRequest req, String id) {
		service.delete(id);
		return JSON.toJSONString(new AjaxRes());
	}
	
	
	/**
	 * 保存排序
	 * @param ids 按顺序排列的多个id
	 */
	@ResponseBody
	@RequestMapping("saveSort")
	public String saveSort(HttpServletRequest req, String ids) {
		service.saveSort(ids);
		return JSON.toJSONString(new AjaxRes());
	}
}
